package com.wc.算法基础课.D第四讲数学知识.约数.Hankson的趣味题;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/4/3 10:10
 * @description https://www.acwing.com/activity/content/problem/content/9870/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 50000, M = 300;
    static int[] primes = new int[N];
    static boolean[] st = new boolean[N];
    // 表示公因数i的个数为j
    static int[][] factor = new int[M][2];
    // 质因数个数
    static int cntf = 0;
    // 所有的约数
    static int[] divider = new int[N];
    // 约束个数
    static int cntd = 0;
    static int idx = 0;
    static int t, a0, a1, b0, b1;

    static void ola() {
        st[0] = st[1] = true;
        int n = N - 1;
        for (int i = 2; i <= n; i++) {
            if (!st[i]) primes[++idx] = i;
            for (int j = 1; primes[j] <= n / i; j++) {
                st[i * primes[j]] = true;
                if (i % primes[j] == 0) break;
            }
        }
    }

    // 思路：找出b1的所有约数，因为x一定是b1的约数
    public static void main(String[] args) {
        ola();
        t = sc.nextInt();
        while (t-- > 0) {
            a0 = sc.nextInt();
            a1 = sc.nextInt();
            b0 = sc.nextInt();
            b1 = sc.nextInt();
            cntf = 0;
            int n = b1;
            for (int i = 1; primes[i] <= n / primes[i]; i++) {
                if (n % primes[i] == 0) {
                    int s = 0;
                    while (n % primes[i] == 0) {
                        s++;
                        n /= primes[i];
                    }
                    factor[++cntf][0] = primes[i];
                    factor[cntf][1] = s;
                }
            }
            if (n > 1) {
                factor[++cntf][0] = n;
                factor[cntf][1] = 1;
            }
            cntd = 0;
            dfs(1, 1);
            int res = 0;
            for (int i = 1; i <= cntd; i++) {
                int x = divider[i];
                if (gcd(x, a0) == a1 && (long) x * b0 / gcd(b0, x) == b1) {
                    res++;
                }
            }
            out.println(res);
        }
        out.flush();
    }

    static void dfs(int u, int p) {
        if (u > cntf) {
            divider[++cntd] = p;
            return;
        }
        for (int i = 0; i <= factor[u][1]; i++) {
            dfs(u + 1, p);
            p *= factor[u][0];
        }
    }

    static int gcd(int a, int b) {
        return b > 0 ? gcd(b, a % b) : a;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
